package org.example.threaddemo;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 定时任务，单线程
 */
public class TimerTaskMain {
    public static void main(String[] args) {
//        t1();
//        t2();
//        t3();
//        t4();
//        t5();
    }

    // 这不是定时任务，开始前热身
    // 如果是false，则会等待子线程结束，整个程序才会结束。
    // 如果是true，主线程结束了，整个程序就结束了，因为子线程是守护线程，不会等待它结束。
    private static void t1() {
        System.out.println("main start");
        Thread thread = new Thread(() -> {
            System.out.println("thread start");
            long beginTime = System.currentTimeMillis();
            while (System.currentTimeMillis() - beginTime < 5 * 1000) {
            }
            System.out.println("thread end");
        });
        thread.setDaemon(false);
        thread.start();
        System.out.println("main end");
    }

    // 调用垃圾回收，否则这个任务不会回收，程序不会停止
    private static void t2() {
        System.out.println("main start");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer run");
                System.gc();
            }
        }, 5000);
        System.out.println("main end");
    }

    // 不调用cancel，这个定时一直会执行下去
    // 只调cancel不调gc，数字会停止输出，但是程序不会停止
    private static void t3() {
        System.out.println("main start");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            private int count = 0;

            @Override
            public void run() {
                System.out.println(count);
                if (++count > 5) {
                    cancel();
                    System.gc();
                }
            }
        }, 5000, 2000);
        System.out.println("main end");
    }

    // 单线程,任务1不完成会阻塞任务2,但是不影响任务2的delay计时
    private static void t4() {
        System.out.println("main start");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Thread thread = Thread.currentThread();
                System.out.println(thread.getId() + " t1 start");
                for (int i = 0; i < 10; i++) {
                    long beginTime = System.currentTimeMillis();
                    while (System.currentTimeMillis() - beginTime < 1 * 1000) {
                    }
                    System.out.println(thread.getId() + " t1 " + i);
                }
                System.out.println(thread.getId() + " t1 end");
                System.gc();
            }
        }, 2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Thread thread = Thread.currentThread();
                System.out.println(thread.getId() + " t2 start");
                for (int i = 0; i < 10; i++) {
                    long beginTime = System.currentTimeMillis();
                    while (System.currentTimeMillis() - beginTime < 1 * 1000) {
                    }
                    System.out.println(thread.getId() + " t2 " + i);
                }
                System.out.println(thread.getId() + " t2 end");
                System.gc();
            }
        }, 2000);
        System.out.println("main end");
    }

    // 任务不结束，period到来时不会执行下一个，会阻塞
    private static void t5() {
        System.out.println("main start");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Thread thread = Thread.currentThread();
                long beginTime = System.currentTimeMillis();
                while (System.currentTimeMillis() - beginTime < 5 * 1000) {
                }
                System.out.println(thread.getId() + " timer run:" + new Date());
            }
        }, 0, 2000);
        System.out.println("main end");
    }
}
